package beastick.utility;

//Copyright 2008 Tofig Kareemov, estereos@gmail.com
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
//http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.


public class CMatcher extends CUtilityMethods
{
  public static boolean StaticMatchesPattern(String sCandidate, String sPattern)
  {
    return (new CMatcher()).MatchesPattern(sCandidate, sPattern);
  }

  public boolean PassesFilters(String sCandidate, String[] sIncludePattern,
    String[] sExcludePattern)
  {
    boolean sResult = false;
    if (IsValuable(sIncludePattern))
    {
      for (int i1 = 0; i1 < sIncludePattern.length; ++i1)
      {
        if (MatchesPattern(sCandidate, sIncludePattern[i1]))
        {
          sResult = true;
          break;
        }
      }
    }
    if (sResult && IsValuable(sExcludePattern))
    {
      for (int i1 = 0; i1 < sExcludePattern.length; ++i1)
      {
        if (MatchesPattern(sCandidate, sExcludePattern[i1]))
        {
          sResult = false;
          break;
        }
      }
    }
    return sResult;
  }

  public boolean MatchesPattern(String sCandidate, String sPattern)
  {
    return MatchesPattern_20040101(sCandidate.toLowerCase(), sPattern.toLowerCase());
  }

  public boolean MatchesPattern_20040101(String sCandidate, String sPattern)
  {
    boolean bResult = true;
    int i = 0;
    int i1 = 0;
    int i2 = 0;
    char cFind = (char) 0;
    char cProcessed = (char) 0;
    String sFind;
    sCandidate = sCandidate.replace('.', '/');
    sCandidate = sCandidate.replace('\\', '/');
    sPattern = sPattern.replace('.', '/');
    sPattern = sPattern.replace('\\', '/');
    for (i = 0; i < sPattern.length(); ++i)
    {
      cFind = sPattern.charAt(i);
      if (cFind == '*')
      {
        ++i;
        sFind = "";
        for (i2 = i; i2 < sPattern.length(); ++i2)
        {
          cProcessed = sPattern.charAt(i2);
          if ((cProcessed != '*') && (cProcessed != '?'))
          {
            sFind += cProcessed;
          }
          else
          {
            break;
          }
        }
        ;
        if (IsValuable(sFind))
        {
          i1 = sCandidate.indexOf(sFind, i1);
          if (i1 < 0)
          {
            bResult = false;
            break;
          }
        }
        else
        {
          i1 = sCandidate.length();
        }
      }
      else if (cFind == '?')
      {
      }
      else if (i1 < sCandidate.length())
      {
        if (sCandidate.charAt(i1) != cFind)
        {
          bResult = false;
          break;
        }
      }
      else
      {
        bResult = false;
        break;
      }
      ++i1;
    }
    ;
    if ((i1 < sCandidate.length()) || (i < sPattern.length()))
    {
      bResult = false;
    }
    ;
    return bResult;
  };
}
